import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

/* Layout */
import Layout from '@/layout';

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/home',
    component: () => import('@/views/home'),
    meta: { title: 'Home', icon: 'home' },
    hidden: true,
    children: [
      {
        path: '/',
        component: () => import('@/views/home/first')
      },
      {
        path: 'login',
        component: () => import('@/views/home/login')
      },
      {
        path: 'register',
        component: () => import('@/views/home/register')
      },
      {
        path: 'faq',
        component: () => import('@/views/home/faq')
      },
      {
        path: 'news',
        component: () => import('@/views/home/news')
      },
      {
        path: 'newsDetial',
        component: () => import('@/views/home/newsDetial')
      },
      {
        path: 'college',
        component: () => import('@/views/home/college')
      },
      {
        path: 'collegeDetial',
        component: () => import('@/views/home/collegeDetial')
      },
      {
        path: 'collegeLearn',
        component: () => import('@/views/home/collegeLearn')
      }
    ]
  },

  {
    path: '/onlineDesign',
    name: 'OnlineDesign',
    component: () => import('@/views/onlineDesign')
  },

  {
    path: '/privacy',
    name: 'Privacy',
    component: () => import('@/views/agreement/privacy')
  },
  {
    path: '/termsofservice',
    name: 'Termsofservice',
    component: () => import('@/views/agreement/termsofservice')
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', icon: 'dashboard' }
      }
    ]
  },

  {
    path: '/store',
    component: Layout,
    redirect: 'shopify',
    meta: { title: 'Store', icon: 'storeSetting' },
    children: [
      {
        path: 'shopify',
        component: () => import('@/views/authorized/shopify'),
        meta: { title: 'Shopify', icon: 'shopify' }
      },
      {
        path: 'wooc',
        component: () => import('@/views/authorized/wooc'),
        meta: { title: 'Wooc', icon: 'woocommerce' }
      },
      {
        path: 'custom',
        component: () => import('@/views/authorized/custom'),
        meta: { title: 'Custom', icon: 'woocommerce' }
      }
    ]
  },

  {
    path: '/orders',
    component: Layout,
    meta: { title: 'Orders', icon: 'order' },
    children: [
      {
        path: '',
        component: () => import('@/views/orders/index'),
        redirect: 'orders',
        hidden: true,
        children: [
          {
            path: 'sync',
            name: 'Sync',
            component: () => import('@/views/orders/sync')
          },
          {
            path: 'orders',
            name: 'Orders',
            hidden: true,
            component: () => import('@/views/orders/orders')
          },
          {
            path: 'produce',
            name: 'Produce',
            hidden: true,
            component: () => import('@/views/orders/produce')
          },
          {
            path: 'packing',
            name: 'Packing',
            hidden: true,
            component: () => import('@/views/orders/packing')
          },
          {
            path: 'shipping',
            name: 'Shipping',
            hidden: true,
            component: () => import('@/views/orders/shipping')
          },
          {
            path: 'delete',
            name: 'Delete',
            hidden: true,
            component: () => import('@/views/orders/delete')
          }
        ]
      }
    ]
  },

  {
    path: '/task-log',
    hidden: true,
    component: Layout,
    children: [
      {
        path: 'list/:key?',
        hidden: true,
        name: 'TaskLog',
        component: () => import('@/views/taskLog')
      }
    ]
  },

  {
    path: '/product',
    redirect: '/product/create',
    component: Layout,
    meta: { title: 'Products', icon: 'product' },
    children: [
      {
        path: 'create',
        component: () => import('@/views/product/create'),
        meta: { title: 'Create Product', icon: 'list' }
      },
      {
        path: 'gather',
        redirect: 'shopify',
        component: () => import('@/views/product/viewer'),
        meta: { title: 'Gather List', icon: 'gather' },
        children: [
          {
            path: 'shopify',
            component: () => import('@/views/product/children/shopify'),
            meta: { title: 'Shopify', icon: 'list' }
          },
          {
            path: 'woocommerce',
            component: () => import('@/views/product/children/wooc'),
            meta: { title: 'WooCommerce', icon: 'list' }
          },
          {
            path: 'shopify_detail/:productId?/:storeId?',
            name: 'sGatherEdit',
            component: () => import('@/views/product/children/shopifyEdit'),
            hidden: true
          },
          {
            path: 'woocommerce_detail/:productId?/:storeId?',
            name: 'wGatherEdit',
            component: () => import('@/views/product/children/woocEdit'),
            hidden: true
          }
        ]
      },
      {
        path: 'collection',
        component: () => import('@/views/product/collection'),
        meta: { title: 'collection', icon: 'product', roles: ['all'] }
      },
      {
        path: 'material',
        redirect: '/product/material/images',
        component: () => import('@/views/product/viewer'),
        meta: { title: 'Material Library', icon: 'materialLibrary' },
        children: [
          {
            path: 'images',
            name: 'MaterialImages',
            component: () => import('@/views/product/children/images'),
            hidden: true
          },
          {
            path: 'design',
            name: 'MaterialDesign',
            component: () => import('@/views/product/children/design'),
            hidden: true
          }
        ]
      }
    ]
  },

  {
    path: '/online',
    component: Layout,
    redirect: 'shopify',
    meta: { title: 'Online Products', icon: 'onlineProducts' },
    children: [
      {
        path: 'shopify',
        component: () => import('@/views/product/viewer'),
        children: [
          {
            path: '',
            name: 'Shopify',
            component: () => import('@/views/product/onlineShopify'),
            meta: { title: 'Shopify', icon: 'shopify' }
          },
          {
            path: 'detail/:storeId?/:productId?',
            name: 'ShopifyDet',
            hidden: true,
            component: () => import('@/views/product/children/shopifyDet')
          }
        ]
      },
      {
        path: 'wooc',
        component: () => import('@/views/product/viewer'),
        children: [
          {
            path: '',
            name: 'Wooc',
            component: () => import('@/views/product/onlineWooc'),
            meta: { title: 'Woocommerce', icon: 'woocommerce' }
          },
          {
            path: 'detail/:storeId?/:productId?',
            name: 'WoocDet',
            hidden: true,
            component: () => import('@/views/product/children/woocDet')
          }
        ]
      },
      {
        path: 'variants/:storeId',
        name: 'Variants',
        hidden: true,
        meta: { title: 'Variants' },
        component: () => import('@/views/product/children/variant')
      }
    ]
  },

  {
    path: '/market',
    component: Layout,
    name: 'Market',
    meta: { title: 'Market', icon: 'product' },
    children: [
      {
        path: 'new',
        name: 'newProduct',
        component: () => import('@/views/market/'),
        meta: { title: 'Market', icon: 'market' }
      }
    ]
  },
  {
    path: '/marketing',
    component: () => import('@/views/market/index'),
    hidden: true,
    children: [
      {
        path: '',
        component: () => import('@/views/market/first'),
        hidden: true
      },
      {
        path: 'cate',
        component: () => import('@/views/market/list'),
        hidden: true
      },
      {
        path: 'detail',
        component: () => import('@/views/market/detail'),
        hidden: true
      },
      {
        path: 'collect',
        component: () => import('@/views/market/collect'),
        hidden: true
      }
    ]
  },

  {
    path: '/logistics',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/logistics/index'),
        meta: { title: 'Logistics', icon: 'logistics' }
      }
    ]
  },

  {
    path: '/finance',
    redirect: '/finance/personal',
    component: Layout,
    meta: { title: 'Finance', icon: 'finance' },
    children: [
      {
        path: 'personal',
        component: () => import('@/views/finance/flowing/personal'),
        meta: { title: 'Personal', icon: 'personal' }
      },
      {
        path: 'disputeAudit',
        component: () => import('@/views/finance/flowing/disputeAudit'),
        meta: { title: 'Dispute', icon: 'disputeAudit' }
      },
      {
        path: 'transaction',
        component: () => import('@/views/finance/flowing/transaction'),
        meta: { title: 'Transaction', icon: 'transaction' }
      },
      {
        path: 'recharge',
        component: () => import('@/views/finance/flowing/recharge'),
        meta: { title: 'Recharge', icon: 'recharge' }
      }
      // {
      //   path: 'appeal',
      //   component: () => import('@/views/finance/appeal/list'),
      //   meta: { title: 'Appeal', icon: 'appeal' }
      // },
      // {
      //   path: 'flowing',
      //   redirect: 'consume',
      //   component: () => import('@/views/finance/flowing'),
      //   meta: { title: 'Flowing', icon: 'flowing' },
      //   children: [
      //     // {
      //     //   path: 'consume',
      //     //   component: () => import('@/views/finance/flowing/consume'),
      //     //   meta: { title: 'Consume', icon: 'consume' }
      //     // },
      //     // {
      //     //   path: 'recharge',
      //     //   component: () => import('@/views/finance/flowing/recharge'),
      //     //   meta: { title: 'Recharge', icon: 'recharge' }
      //     // },

      //   ]
      // }
    ]
  },

  {
    path: '/help',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/help/index'),
        meta: { title: 'Help', icon: 'help' }
      }
    ]
  },

  {
    path: '/shopify/oauth/success',
    component: () => import('@/views/callback/shopifySucc'),
    hidden: true
  },

  {
    path: '/shopify/oauth/fail',
    component: () => import('@/views/callback/shopifyFail'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
];

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
